package com.liang.maogouyuyin.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.liang.maogouyuyin.bean.ArticleBean
import com.liang.maogouyuyin.databinding.ItemTrainingBinding
import com.liang.maogouyuyin.ui.activity.WebActivity

class TrainingAdapter(val context: Context): RecyclerView.Adapter<TrainingAdapter.ViewHolder>() {
    class ViewHolder(binding: ItemTrainingBinding): RecyclerView.ViewHolder(binding.root) {
        val itemTitle = binding.itemTrainingTitle
        val itemContent = binding.itemTrainingContent
        val itemImage = binding.itemTrainingImage
    }

    private lateinit var list: ArrayList<ArticleBean>

    private var isAnimal = false

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemTrainingBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = list[position]
        holder.itemTitle.text = bean.title
        holder.itemContent.text = bean.content
        holder.itemView.setOnClickListener {
            val intent = Intent(context, WebActivity::class.java)
            if (isAnimal) {
                intent.putExtra("title", bean.title)
                intent.putExtra("url", bean.url)
                intent.putExtra("type", 2)
            } else {
                intent.putExtra("title", bean.title)
                intent.putExtra("url", bean.url)
                intent.putExtra("type", 1)
            }
            context.startActivity(intent)
        }
    }

    fun setList(list: ArrayList<ArticleBean>) {
        this.list = list
    }

    fun setAnimal(flag: Boolean) {
        this.isAnimal = flag
    }
}